<template>
  <div class="app-container">
    <el-card class="search-card">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
          <el-form-item label="学院" prop="depId">
            <el-select v-model="queryParams.depId" @change="depChange" placeholder="请选择学院" clearable>
              <el-option
                v-for="item in departmentList"
                :key="item.deptId"
                :label="item.deptName"
                :value="item.deptId"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="专业" prop="subjectId">
            <el-select v-model="queryParams.subjectId" placeholder="请选择专业" clearable>
              <el-option
                v-for="item in subjectList"
                :key="item.id"
                :label="item.subjectName"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="培养方案" prop="trainingProgramTitle">
            <el-input
              v-model="queryParams.trainingProgramTitle"
              placeholder="请输入培养方案标题"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="年级" prop="grade">
            <el-input
              v-model="queryParams.grade"
              placeholder="请输入年级"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="是否启用" prop="isEnable">
            <el-select v-model="queryParams.isEnable" placeholder="请选择是否启用：0-未启用  1-启用" clearable>
              <el-option
                v-for="dict in dict.type.sys_common_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
    </el-card>

    <el-card class="table-card">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['train:trainingProgram:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['train:trainingProgram:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['train:trainingProgram:remove']"
            >删除</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading"
                  row-key="id"
                  :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
                  :data="trainingProgramList"
                  @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="序号" align="center" prop="id" />
          <el-table-column label="学院" align="center" prop="depName" />
          <el-table-column label="专业" align="center" prop="subjectName" />
          <el-table-column label="培养方案" align="center" prop="trainingProgramTitle" />
          <el-table-column label="年级" align="center" prop="grade" />
          <el-table-column label="毕业总学分" align="center" prop="totalCreditsGraduation" />
          <el-table-column label="毕业总学绩分" align="center" prop="totalAcademicPerformanceScore" />
          <el-table-column label="课外学分" align="center" prop="extracurricularCredits" />
          <el-table-column label="是否无处分" align="center" prop="isDisciplinaryAction">
            <template slot-scope="scope">
              <el-tag :type="scope.row.isDisciplinaryAction == 1 ? 'success' : 'info'">
                {{scope.row.isDisciplinaryAction == 1 ? '是' : '否'}}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="启用状态" align="center" prop="isEnable">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_common_status" :value="scope.row.isEnable"/>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['train:trainingProgram:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['train:trainingProgram:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
    </el-card>

    <!-- 添加或修改专业培养方案对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="培养方案" prop="trainingProgramTitle">
          <el-input v-model="form.trainingProgramTitle" placeholder="请输入培养方案标题" />
        </el-form-item>
        <el-form-item label="学院" prop="depId">
          <el-select v-model="form.depId" @change="depChange" placeholder="请选择学院" clearable>
            <el-option
              v-for="item in departmentList"
              :key="item.deptId"
              :label="item.deptName"
              :value="item.deptId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="专业" prop="subjectId">
          <el-select v-model="form.subjectId" placeholder="请选择专业" clearable>
            <el-option
              v-for="item in subjectList"
              :key="item.id"
              :label="item.subjectName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="方案内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="年级" prop="grade">
          <el-input v-model="form.grade" placeholder="年级" />
        </el-form-item>
        <el-form-item label="毕业总学分" prop="totalCreditsGraduation">
          <el-input-number v-model="form.totalCreditsGraduation" :min="1" :precision="0" label="请输入毕业总学分" />&nbsp;分
        </el-form-item>
        <el-form-item label="毕业总学绩分" prop="totalAcademicPerformanceScore">
          <el-input-number :min="1" :precision="0" v-model="form.totalAcademicPerformanceScore" placeholder="请输入毕业总学绩分：学分乘以绩点" />&nbsp;分
        </el-form-item>
        <el-form-item label="课外学分" prop="extracurricularCredits">
          <el-input-number  :min="1" :precision="0" v-model="form.extracurricularCredits" placeholder="请输入课外学分" />&nbsp;分
        </el-form-item>
        <el-form-item label="是否无处分" prop="isDisciplinaryAction">
          <el-radio-group v-model="form.isDisciplinaryAction">
            <el-radio :label="0">否</el-radio>
            <el-radio :label="1">是</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="启用状态" prop="isEnable">
          <el-radio-group v-model="form.isEnable">
            <el-radio
              v-for="dict in dict.type.sys_common_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTrainingProgram, getTrainingProgram, delTrainingProgram, addTrainingProgram, updateTrainingProgram } from "@/api/train/trainingProgram";
import { getCollege } from '@/api/system/dept'
import { getSubjectByCollegeId } from '@/api/information/subject'

export default {
  name: "TrainingProgram",
  dicts: ['sys_common_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 专业培养方案表格数据
      trainingProgramList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        depId: null,
        subjectId: null,
        trainingProgramTitle: null,
        grade: null,
        isEnable: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        depId: [
          { required: true, message: "学院不能为空", trigger: "change" }
        ],
        subjectId: [
          { required: true, message: "专业不能为空", trigger: "change" }
        ],
        trainingProgramTitle: [
          { required: true, message: "培养方案标题不能为空", trigger: "blur" }
        ],
        grade: [
          { required: true, message: "年级不能为空", trigger: "change" }
        ],
        content: [
          { required: true, message: "培养方案内容不能为空", trigger: "blur" }
        ],
        totalCreditsGraduation: [
          { required: true, message: "毕业总学分不能为空", trigger: "blur" }
        ],
        totalAcademicPerformanceScore: [
          { required: true, message: "毕业总学绩分：学分乘以绩点不能为空", trigger: "blur" }
        ],
        extracurricularCredits: [
          { required: true, message: "课外学分不能为空", trigger: "blur" }
        ],
        isDisciplinaryAction: [
          { required: true, message: "是否无处分：0：不做要求，1：毕业时无任何处分不能为空", trigger: "change" }
        ],
        isEnable: [
          { required: true, message: "是否启用：0-未启用  1-启用不能为空", trigger: "change" }
        ],
      },

      departmentList: [],
      subjectList: [],
    };
  },
  created() {
    this.getList();
    this.findCollege()
    this.findSubjectByCollegeId({depId: null})
  },
  methods: {
    findCollege(){
      getCollege().then(res => {
        this.departmentList = res.data
      })
    },
    depChange(val){
      this.findSubjectByCollegeId({depId: val})
    },
    // 根据学院ID查询专业信息
    findSubjectByCollegeId(data) {
      getSubjectByCollegeId(data).then(res =>{
        this.subjectList = res.data
      })
    },

    /** 查询专业培养方案列表 */
    getList() {
      this.loading = true;
      listTrainingProgram(this.queryParams).then(response => {
        this.trainingProgramList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        depId: null,
        subjectId: null,
        trainingProgramTitle: null,
        grade: null,
        content: null,
        totalCreditsGraduation: null,
        totalAcademicPerformanceScore: null,
        extracurricularCredits: null,
        isDisciplinaryAction: null,
        isEnable: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加专业培养方案";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTrainingProgram(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改专业培养方案";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTrainingProgram(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTrainingProgram(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除专业培养方案编号为"' + ids + '"的数据项？').then(function() {
        return delTrainingProgram(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('train/trainingProgram/export', {
        ...this.queryParams
      }, `trainingProgram_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
